using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class ThuocDAO
    {
        public static bool Insert(ThuocDTO t)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@DonGia", t.DonGia));
                sqlparam.Add(new SqlParameter("@MaThuoc", t.MaThuoc));
                sqlparam.Add(new SqlParameter("@TenThuoc", t.TenThuoc));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spThuocInsert",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(ThuocDTO t)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
               
                sqlparam.Add(new SqlParameter("@MaThuoc", t.MaThuoc));
              
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spThuocDelete",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Update(ThuocDTO t)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@DonGia", t.DonGia));
                sqlparam.Add(new SqlParameter("@MaThuoc", t.MaThuoc));
                sqlparam.Add(new SqlParameter("@TenThuoc", t.TenThuoc));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spThuocUpdate",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<ThuocDTO> Select()
        {
            List<ThuocDTO> lst = new List<ThuocDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spThuocSelect");
                
                foreach (DataRow dr in dt.Rows)
                {
                    ThuocDTO t = new ThuocDTO();
                    t.MaThuoc = int.Parse(dr["MaThuoc"].ToString());
                    t.TenThuoc = (dr["TenThuoc"].ToString());
                    t.DonGia = float.Parse(dr["DonGia"].ToString());
                    lst.Add(t);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;
        }
        public static ThuocDTO SelectByID(int MaThuoc)
        {
            ThuocDTO t = new ThuocDTO();
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaThuoc", MaThuoc));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spThuocSelectByID",sqlparam);
                DataRow dr = dt.Rows[0];
                                  
                t.MaThuoc = int.Parse(dr["MaThuoc"].ToString());
                t.TenThuoc = (dr["TenThuoc"].ToString());
                t.DonGia = float.Parse(dr["DonGia"].ToString());                
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return t;
        }
        
    }
}
